package com.hzw.saas.common.util.enums;

import java.util.Objects;

import cn.hutool.core.lang.Assert;

/**
 * 存储空间
 *
 * @author zzl
 * @since 02/26/2021
 */
public enum TimeUnitEnum {

    SECOND(1, "s"),
    MINUTE(SECOND.value() * 60, "m"),
    HOUR(MINUTE.value() * 60, "H"),
    DAY(HOUR.value() * 24, "D"),
    WEEK(DAY.value() * 7, "W"),

    // 以下value为参考值，不建议进行计算
    MONTH(DAY.value() * 30, "M"),
    SEASON(MONTH.value() * 3, "S"),
    YEAR(DAY.value() * 365, "Y"),
    ;

    /**
     * 单位时间相对于秒的换算值
     */
    private final Integer value;
    private final String text;

    TimeUnitEnum(Integer value, String text) {
        this.value = value;
        this.text = text;
    }

    public Integer value() {
        return value;
    }

    public String text() {
        return text;
    }

    public static TimeUnitEnum instance(String text) {
        if (!Objects.equals(text, TimeUnitEnum.SECOND.text) && !Objects.equals(text, TimeUnitEnum.MINUTE.text)) {
            text = text.toUpperCase();
        }
        for (TimeUnitEnum value : TimeUnitEnum.values()) {
            if (Objects.equals(value.text, text))
                return value;
        }
        return null;
    }

    public static TimeUnitEnum assertInstance(String text) {
        TimeUnitEnum instance = instance(text);
        Assert.notNull(instance, "时间单位异常：{}", text);
        return instance;
    }

}
